package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BlogDao {
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;

    //1、往博客表里插入博客
    public void insert(Blog blog) {
        try {
            connection = DBUtil.getConnection();//建立连接是静态方法
            String sql = "insert into blog values(null, ?, ?, ?, now())";
            statement = connection.prepareStatement(sql);//建立连接第二步
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getUserId());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

    //2、获取博客表中所有博客的信息
    public List<Blog> selectAll(){//这里获取所有的博客要用一个集合接受
        List<Blog> list = new ArrayList<>();
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();//select的写法
            while (resultSet.next()){
                //创建一个对象，把获取的值赋给这个对象
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));//获取数据的方法，利用resultSet将数据库中对应字符串的值获取
                blog.setTitle(resultSet.getString("title"));
                //blog.setContent(resultSet.getString("content"));
                //这里只是为了获取内容大概
                String content = resultSet.getString("content");
                String regEx= "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*()——+|{}【】‘；：”“’。，、？\\\\]";
                if (content.length() > 50){
                    content = content.substring(0,50) + "...";
                }
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(content);
                content = m.replaceAll(" ").trim();
                //这里别忘了setContent
                blog.setContent(content);
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                list.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return list;
    }

    //3、根据博客ID获取具体的博客
    public Blog selectOne(int blogId){
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            resultSet = statement.executeQuery();
            if (resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                return blog;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }
    //4、根据id删除博客
    public void delete(int blogId){
        try {
            connection = DBUtil.getConnection();
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }
}
